﻿using System;
using System.Collections.Generic;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using NHibernate;

namespace Cms.Data
{
    public class LocaleRepository : BaseRepository, ILocaleRepository
    {
        public LocaleRepository()
        {
        }

        public LocaleRepository(ISession session)
            : base(session)
        {
        }

        #region ILocaleRepository Members

        public IList<Locale> GetLocales()
        {
            return Session.CreateCriteria(typeof(Locale)).SetCacheable(true).List<Locale>();
        }

        public Locale GetLocale(int id)
        {
            return Session.Get<Locale>(id);
        }

        public void DeleteLocale(Locale locale)
        {
            ITransaction tx = null;
            try
            {
                tx = Session.BeginTransaction();
                Session.CreateSQLQuery("DELETE FROM cmsPageContentSetTranslationVersion WHERE ContentSetID IN " +
                                       "(SELECT ID FROM cmsPageContentSet WHERE LocaleID = " + locale.ID + ")")
                    .ExecuteUpdate();
                Session.CreateSQLQuery("DELETE FROM cmsPageContentSet WHERE LocaleID = " + locale.ID).ExecuteUpdate();
                Session.CreateSQLQuery("DELETE FROM cmsMasterTemplateContentSetTranslationVersion WHERE ContentSetID IN " +
                                        "(SELECT ID FROM cmsMasterTemplateContentSet WHERE LocaleID = " + locale.ID + ")")
                    .ExecuteUpdate();
                Session.CreateSQLQuery("DELETE FROM cmsMasterTemplateContentSet WHERE LocaleID = " + locale.ID).ExecuteUpdate();
                Session.CreateSQLQuery("DELETE FROM cmsDetailTemplateContentSetTranslationVersion WHERE ContentSetID IN " +
                                        "(SELECT ID FROM cmsDetailTemplateContentSet WHERE LocaleID = " + locale.ID + ")")
                    .ExecuteUpdate();
                Session.CreateSQLQuery("DELETE FROM cmsDetailTemplateContentSet WHERE LocaleID = " + locale.ID).ExecuteUpdate();
                Session.CreateSQLQuery("DELETE FROM cmsContentTranslationVersion WHERE TranslationID IN " +
                                       "(SELECT ID FROM cmsContentTranslation WHERE LocaleID = " + locale.ID + ")")
                    .ExecuteUpdate();
                Session.CreateSQLQuery("DELETE FROM cmsContentTranslation WHERE LocaleID = " + locale.ID).ExecuteUpdate();
                foreach (Locale l in locale.Site.Locales)
                {
                    l.Translations.Remove(locale.ID);
                }
                locale.Site.Locales.Remove(locale);
                locale.Site = null;
                Session.Delete(locale);
                Session.Flush();
                tx.Commit();
            }
            catch (Exception ex)
            {
                RollbackTransaction("LocaleRepository.DeleteLocale", tx, ex);
                throw;
            }
        }

        public void SaveLocale(Locale locale)
        {
            SaveEntity(locale, "LocaleRepository.SaveLocale");
        }

        #endregion
    }
}